﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Restaurant.DAO;
using Restaurant.DTO;
using System.Data;


namespace Restaurant.BUS
{
    class KhachHangService
    {
        private KhachHangDB khDAO = new KhachHangDB();

        public DataTable LoadTatCaKH()
        {
            return khDAO.LoadKhachHang();
        }

        public bool ThemKH(KhachHang kh)
        {
            if (kh.TenKH == "" || kh.CMND == "" || kh.SDT == "")
            {
                return false;
            }
            if (khDAO.ThemKhachHang(kh.TenKH, kh.CMND, kh.SDT))
                return true;
            return false;
        }

        public bool SuaKH(KhachHang kh)
        {
            if (khDAO.SuaKhachHang(kh.MaKH, kh.TenKH, kh.CMND, kh.SDT))
                return true;
            return false;
        }

        public bool XoaKH(int ma)
        {
            if (khDAO.XoaKhachHang(ma))
                return true;
            return false;
        }

        public KhachHang TimKHTen(string ten,string cmnd)
        {
            KhachHang kh = new KhachHang();
            DataTable dbtab = khDAO.TimKhachHangTheoTen(ten,cmnd);
            DataRow dbrow = dbtab.Rows[0];
            kh.MaKH = int.Parse(dbrow[0].ToString());
            kh.TenKH = dbrow[1].ToString();
            kh.CMND = dbrow[2].ToString();
            kh.SDT = dbrow[3].ToString();
            return kh;
        }

        public KhachHang TimKHID(int ma)
        {
            KhachHang kh = new KhachHang();
            DataTable dbtab = khDAO.TimKhachHangID(ma);
            DataRow dbrow = dbtab.Rows[0];
            kh.MaKH = int.Parse(dbrow[0].ToString());
            kh.TenKH = dbrow[1].ToString();
            kh.CMND = dbrow[2].ToString();
            kh.SDT = dbrow[3].ToString();
            return kh;
        }

        public KhachHang TimKHTheoBan(int maban)
        {
            KhachHang kh = new KhachHang();
            DataTable dbtab = khDAO.TimKHTheoBan(maban);
            DataRow dbrow = dbtab.Rows[0];
            kh.MaKH = int.Parse(dbrow["MaKH"].ToString());
            kh.TenKH = dbrow["TenKH"].ToString();
            kh.CMND = dbrow["CMND"].ToString();
            kh.SDT = dbrow["SDT"].ToString();
            kh.Soluong = int.Parse(dbrow["SoLuongNguoi"].ToString());
            return kh;
        }
    }
}
